package com.javasm.service;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.javasm.entity.User;
import com.javasm.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {

//    @Autowired
//    private UserMapper userMapper;

    @Autowired
    private UserMapper userMapper;
    public User getByUserName(String username) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("username", username);
        List<User> users = userMapper.selectByMap(map);
        if (CollectionUtils.isEmpty(users)) {
            return null;
        }
        return users.get(0);
    }

    public User getByPhone(String phone) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("phone", phone);
        List<User> users = userMapper.selectByMap(map);
        if (CollectionUtils.isEmpty(users)) {
            return null;
        }
        return users.get(0);
    }

    @Autowired
    private PasswordEncoder passwordEncoder;

    public void updatePassword(String username, String passwrod) {
        User user = getByUserName(username);
        user.setPassword(passwordEncoder.encode(passwrod));
        userMapper.updateById(user);
    }


}